require('../css/device.scss')
define(['../deviceModule'], function(module) {
	module.controller("workDeviceCtrl", ['$scope', '$rootScope', '$state', 'httpService', '$timeout', 'ENV', 'growl', 'ngDialog',
		function($scope, $rootScope, $state, httpService, $timeout, ENV, growl, ngDialog) {
		'use strict';
		$scope.searchData = {
			key: 'simNo',
            value: ''
		};
		console.log(1);
            // 表格对象
		$scope.tableConf = {
			"tableHead": [{
				"name": "序号",
				"width": "1"
			}, {
				"name": "微信用户openid",
				"width": "1"
			}, {
				"name": "设备号",
				"width": "1"
			}, {
				"name": "设备的sim号",
				"width": "1"
			}, {
				"name": "湿度",
				"width": "1"
			},{
				"name": "温度",
				"width": "1"
			},{
				"name": "档位",
				"width": "1"
			},{
				"name": "机器开机状态",
				"width": "1"
			},{
				"name": "定时起始时间",
				"width": "1"
			},{
				"name": "定时终止时间",
				"width": "1"
			},{
				"name": "定位信息",
				"width": "2"
			}],
			"tableTr": []
		}

		// 分页配置
		$scope.conf = {
			currentPage: 1,
			total: 0,
			itemPageLimit: 20,
			// 是否显示一页选择多少条
			isSelectPage: true,
			// 是否显示快速跳转
			isLinkPage: false
		}

		// 赋值
		function assignFn(data) {
			// 表格数据
			$scope.tableConf.tableTr = data.workDeviceList;
			// 分页数据
			$scope.conf.total = parseInt(data.totalRow);
			$scope.conf.currentPage = parseInt(data.pageNum);
			$scope.conf.itemPageLimit = parseInt(data.pageSize);
		}
		//清除
		function resetTable() {
			$scope.tableConf.tableTr = [];
			$scope.conf.total = 0;
			$scope.conf.currentPage = 1;
		}

		// 分页请求
		$scope.$watch('conf.currentPage', function(newV, oldV) {
			if (newV != oldV && oldV != 0 && newV != 0) {
				var data = {pageNum: newV};
				if ($scope.searchData.key) data[$scope.searchData.key] = $scope.searchData.value;
				httpService.linkHttp({
					url: ENV.deviceWorkList
				}, data).then(function(res) {
					// 判断请求成功
					var result = res.data;
					if (result.code == "000000" && result.data) {
						assignFn(result.data);
					}else {
                        resetTable();
                        var errText;
                        result.errDesc ? errText = result.errDesc : errText = '请求获取失败, 请稍后再尝试';
                        growl.addErrorMessage(errText);
					}
				});
			}
		});
		// 首次加载
		httpService.linkHttp({
			url: ENV.deviceWorkList,
			isErrMsgFn: function() {}
		}).then(function(res) {
			// 判断请求成功
			var result = res.data;
			if (result.code == "000000" && result.data) {
				assignFn(result.data);
			} else {
                resetTable();
                var errText;
                result.errDesc ? errText = result.errDesc : errText = '请求获取失败, 请稍后再尝试';
                growl.addErrorMessage(errText);
			}
		});
		// 查询操作
		$scope.workDevSearch = function() {
			if ($scope.searchData.key) data[$scope.searchData.key] = $scope.searchData.value;
			if ($scope.searchData.value && !$scope.searchData.key)  {
				growl.addErrorMessage("请选择查询关键词");
			} else {
				httpService.linkHttp({
					url: ENV.deviceWorkList
				}, data).then(function(res) {
					var result = res.data;
					if (result.code == "000000" && result.data) {
						assignFn(result.data);
					} else {
                        resetTable();
                        var errText;
                        result.errDesc ? errText = result.errDesc : errText = '请求获取失败, 请稍后再尝试';
                        growl.addErrorMessage(errText);
					}
				})
			}

		}



	}])
});
